package org.oneblog.data.util;

import java.util.ArrayList;
import java.util.Collection;
import java.util.List;

import org.apache.solr.common.SolrDocument;
import org.springframework.util.CollectionUtils;

/**
 *
 * @author Smile.Wu
 * @version 2015-11-2
 */
public class PojoBeanCreator {

	public static String getDocValue(Object v) {
		if(v == null) {
			return "";
		}
		return v.toString();
	}
	public static long getLongValue(SolrDocument d, String f, long def) {
		try {
			return StringTools.getLong(getDocValue(d.get(f)), def);
		} catch (Exception e) {
			return def;
		}
	}
	public static double getDoubleValue(SolrDocument d, String f, double def) {
		try {
			return StringTools.getDbl(getDocValue(d.get(f)), def);
		} catch (Exception e) {
			return def;
		}
	}
	public static int getIntValue(SolrDocument d, String f, int def) {
		try {
			return StringTools.getInt(getDocValue(d.get(f)), def);
		} catch (Exception e) {
			return def;
		}
	}
	
	public static List<Long> getListValue(SolrDocument d , String f){
		try{
			List<Long> list = new ArrayList<>();
			Collection<Object> c = d.getFieldValues(f);
			if  (!CollectionUtils.isEmpty(c)){
				for(Object o : c){

					long id = StringTools.getLong(o, 0);
					if (id > 0){
						list.add(id);
					}
				}
			}
			return list;
		}catch (Exception e){
			return new ArrayList<>(0);
		}
	}

	public static List<String> getStringValueList(SolrDocument d , String f){
		try{
			List<String> list = new ArrayList<>();
			Collection<Object> c = d.getFieldValues(f);
			if  (!CollectionUtils.isEmpty(c)){
				for(Object o : c){
					if(o != null) {
						list.add(o.toString());
					}
				}
			}
			return list;
		}catch (Exception e){
			return new ArrayList<>(0);
		}
	}

}
